package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsAgentInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsAgentInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/*
 *代理机构信息接口
 * @author Wuych on 2020/9/7 11:09
 **/
public class CopsAgentInfoMapperImpl implements CopsAgentInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    /**
     * 根据主键查询商户结算账户信息
     *
     * @param instId    多法人标识
     * @param agentCode 代理机构号
     * @return 代理机构信息
     * @throws Exception 异常
     */
    @Override
    public CopsAgentInfo selectAgentInfoByPrimaryKey(String instId, String agentCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsSelectAgentInfoByPrimaryKey");
        CopsAgentInfo copsAgentInfo = new CopsAgentInfo();
        copsAgentInfo.setInstId(instId);
        copsAgentInfo.setAgentCode(agentCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, copsAgentInfo, copsAgentInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.info("查询代理机构信息表不存在，instId:<{}>,agentCode:<{}>", instId, agentCode);
            return null;
        } else if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.info("查询代理机构信息表失败，instId:<{}>,agentCode:<{}>", instId, agentCode);
            return null;
        } else {

        }
        return copsAgentInfo;
    }
}
